//
//  DYInputMoneyView.m
//  HBuilder
//
//  Created by 黄德玉 on 2019/11/2.
//  Copyright © 2019 DCloud. All rights reserved.
//

#import "DYInputMoneyView.h"
#import "DYGlobal.h"
#import "Masonry.h"

@interface DYInputMoneyView ()<UITextFieldDelegate>


@property (nonatomic,strong) UITextField * textField;
@property (nonatomic, strong) UIButton * sureBtn;
@property (nonatomic, strong) UIControl * bgControl;
@property (nonatomic, copy) void (^didCallback)(id value);


@end

@implementation DYInputMoneyView

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"释放123123131231");
}

+ (void)showWithCallBack:(void (^)(id _Nonnull))callback{
    DYInputMoneyView * v = [DYInputMoneyView new];
    [[UIApplication sharedApplication].keyWindow addSubview:v];
    v.didCallback = callback;
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(v.superview);
    }];
    [v.textField becomeFirstResponder];
}

- (instancetype)init{
    if (self = [super init]) {
        self.bgControl = [UIControl new];
        [self.bgControl  addTarget:self action:@selector(bgTouched) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.bgControl];
        [self.bgControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.sureBtn setTitle:@"确认" forState:UIControlStateNormal];
        self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        self.sureBtn.backgroundColor = HexRGB(0xad89f9);
        self.sureBtn.layer.cornerRadius = 5;
        self.sureBtn.layer.masksToBounds = YES;
        [self.sureBtn addTarget:self action:@selector(sureClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.sureBtn];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-16);
            make.bottom.equalTo(self).offset(-10);
            make.width.equalTo(@56);
            make.height.equalTo(@30);
        }];
        
        self.textField = [[UITextField alloc] init];
        self.textField.backgroundColor = HexRGB(0xF4F4F4);
        self.textField.layer.cornerRadius = 5;
        self.textField.layer.masksToBounds = YES;
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
        self.textField.returnKeyType = UIReturnKeyDone;
        self.textField.delegate = self;
        [self addSubview:self.textField];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(16);
            make.right.equalTo(self.sureBtn.mas_left).offset(-20);
            make.centerY.equalTo(self.sureBtn);
            make.height.equalTo(@30);
        }];
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];

    }
    return self;
}


- (void)bgTouched{
    [self endEditing:YES];
    if (self.didCallback) {
        self.didCallback(self.textField.text);
    }
}

- (void)sureClicked{
    [self endEditing:YES];
    if (self.didCallback) {
          self.didCallback(self.textField.text);
      }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self endEditing:YES];
    return YES;
}

#pragma mark --键盘弹出
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
        //取出键盘动画的时间(根据userInfo的key----UIKeyboardAnimationDurationUserInfoKey)
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    //取得键盘最后的frame(根据userInfo的key----UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 227}, {320, 253}}";)
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    //计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.size.height;
    self.transform = CGAffineTransformIdentity;
    //执行动画
    [UIView animateWithDuration:duration animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, - transformY);
    }];
}
#pragma mark --键盘收回
- (void)keyboardDidHide:(NSNotification *)notification{
    CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}



@end
